<template>
    <tnxel-submit-form ref="form" :model="model" :rules="rules" label-width="126">
        <el-form-item label="父工程">
            <MavenProjectPath :project="parent"/>
        </el-form-item>
        <el-form-item label="groupId" prop="groupId">
            <el-col :span="12">
                <el-input v-model="model.groupId" placeholder="请输入" @input="autoLayerGroupId"/>
            </el-col>
        </el-form-item>
        <el-form-item label="artifactId" prop="artifactId">
            <el-col :span="12">
                <el-input v-model="model.artifactId" placeholder="请输入" @input="autoLayerGroupId"/>
            </el-col>
        </el-form-item>
        <el-form-item label="作为基础模块">
            <el-switch v-model="model.basic"/>
            <span class="form-item-tip text-warning"
                v-if="model.basic && basicModule && model.artifactId !== basicModule.artifactId">
                原基础模块 <strong>{{ basicModule.artifactId }}</strong> 将被转换为普通模块，对其的依赖仍然保留
            </span>
            <div class="form-item-tip">
                基础模块包含基础公共类和配置，将被所有其它模块直接或间接引用，整个工程最多只能有一个基础模块
            </div>
        </el-form-item>
        <el-form-item label="依赖模块" v-if="!model.basic && dependableModules.length">
            <tnxel-select v-model="model.dependencyModuleArtifactIds"
                :items="dependableModules"
                value-name="artifactId"
                text-name="artifactId"
                selector="checkbox"
            />
            <div class="form-item-tip">
                如果选择依赖模块，则以下所选层级除api外，均将依次依赖所有依赖模块的对应层级。基础模块会自动依赖，不在此列。
            </div>
        </el-form-item>
        <LayerForm ref="layerForm" :root="parent" :parent="model" :dependency-modules="dependencyModules"/>
    </tnxel-submit-form>
</template>

<script>
import tnx from '@/tnx.js';
import ProjectUtil from '@/logic/maven/util/project.js';

import MavenProjectPath from './components/MavenProjectPath.vue';
import LayerForm from './components/LayerForm.vue';

export default {
    components: {MavenProjectPath, LayerForm},
    props: {
        parent: {
            type: Object,
            required: true,
        },
        parentPomPath: {
            type: String,
            required: true,
        },
        after: {
            type: Function,
            required: true,
        },
    },
    data() {
        let groupId = this.parent.groupId;
        if (this.parent.groupId !== this.parent.artifactId && !groupId.endsWith('.' + this.parent.artifactId)) {
            groupId += '.' + this.parent.artifactId;
        }
        return {
            model: {
                groupId: groupId,
                artifactId: '',
                basic: false,
                layerGroupId: '',
                modules: [],
                dependencyModuleArtifactIds: [],
            },
            rules: {
                groupId: [tnx.app.validator.requiredRule],
                artifactId: [tnx.app.validator.requiredRule,
                    {
                        validator: (rule, value, callback) => {
                            if (this.parent.modules) {
                                let existedModule = this.parent.modules.find(m => m.artifactId === value);
                                if (existedModule && !existedModule.removed) {
                                    callback(new Error('已存在同名模块'));
                                }
                            }
                            callback();
                        },
                        trigger: 'blur',
                    }
                ],
            },
        };
    },
    computed: {
        basicModule() {
            if (this.parent.modules) {
                for (let module of this.parent.modules) {
                    if (module.basic) {
                        return module;
                    }
                }
            }
            return null;
        },
        dependableModules() {
            let modules = [];
            for (let module of this.parent.modules) {
                if (module.packaging === 'pom' && !module.basic) {
                    modules.push(module);
                }
            }
            return modules;
        },
        dependencyModules() {
            let modules = [];
            for (let dependencyModuleArtifactId of this.model.dependencyModuleArtifactIds) {
                let module = this.dependableModules.find(m => m.artifactId === dependencyModuleArtifactId);
                if (module) {
                    modules.push(module);
                }
            }
            return modules;
        },
    },
    methods: {
        dialog() {
            return {
                title: '添加标准模块',
                width: 560,
                type: 'confirm',
                click: this.toSubmit,
            };
        },
        autoLayerGroupId() {
            let layerGroupId = ProjectUtil.getDefaultGroupId(this.model);
            if (layerGroupId) {
                this.$refs.layerForm.model.layerGroupId = layerGroupId;
                this.$refs.layerForm.validate();
            }
        },
        toSubmit(yes, close) {
            if (yes) {
                this.$refs.form.toSubmit(() => {
                    this.$refs.layerForm.toSubmit(yes, subModules => {
                        if (this.model.basic && this.parent.modules) {
                            this.parent.modules.forEach(m => m.basic = false);
                        }
                        this.after({
                            groupId: this.model.groupId,
                            artifactId: this.model.artifactId,
                            packaging: 'pom',
                            modules: subModules,
                            basic: this.model.basic,
                        });
                        close();
                    });
                });
                return false;
            }
        },
    },
}
</script>

<style lang="scss" scoped>
</style>
